public class Permutation {
  public int numPermutation (String str) {
   char[] chars = str.toCharArray();
   Arrays.sort(chars);
   StringBuffer out = new StringBuffer();
 
   return doPermutation(chars, out, 0);
  }
  
  public void doPermutation(char[] chars, StringBuffer out, int startIndex) {
    if(out.toString.length() == chars.length)
      System.out.println(out.toString);
    if(startIndex >= chars.length )
      break;
      
    for(int i = startIndex; i < chars.length; i++) {
      out.append(chars[i])
      
      doPermutation(chars, out, ++startIndex);
      
      out.setLength(out.length()-1);
    }
  }
}